package service;

import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;



@WebServlet(urlPatterns = {"/save"})
public class saveService extends HttpServlet {
    public boolean c2dictionary;
    public save data;


    public saveService() {
        super();
        // TODO Auto-generated constructor stub
    }

    public saveService(save res){
        this.c2dictionary = true;
        this.data = res;
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html; charset=utf-8");
        PrintWriter out = response.getWriter();
        try {
            response.setHeader("Cache-Control", "no-store");
            response.setHeader("Pragma", "no-cache");
            response.setHeader("Access-Control-Allow-Origin", "*");
            response.setDateHeader("Expires", 0);
            String user = request.getParameter("user");
            String day = request.getParameter("day");
            String hp = request.getParameter("hp");
            String place = request.getParameter("place");
            String cash = request.getParameter("cash");
            String bank = request.getParameter("bank");
            String guangpanp = request.getParameter("guangpanp");
            String guangpanq = request.getParameter("guangpanq");
            String xiangyanp = request.getParameter("xiangyanp");
            String xiangyanq = request.getParameter("xiangyanq");
            String huazhuangp = request.getParameter("huazhuangp");
            String huazhuangq = request.getParameter("huazhuangq");
            String wanjup = request.getParameter("wanjup");
            String wanjuq = request.getParameter("wanjuq");
            String shoujip = request.getParameter("shoujip");
            String shoujiq = request.getParameter("shoujiq");
            String jiajiup = request.getParameter("jiajiup");
            String jiajiuq = request.getParameter("jiajiuq");
            String avp = request.getParameter("avp");
            String avq = request.getParameter("avq");
            String qichep = request.getParameter("qichep");
            String qicheq = request.getParameter("qicheq");
            String guwanp = request.getParameter("guwanp");
            String guwanq = request.getParameter("guwanq");
            String fangzip = request.getParameter("fangzip");
            String fangziq = request.getParameter("fangziq");
            save.saveToSQL(user,day,hp,place,cash,bank,guangpanp,guangpanq,xiangyanp,xiangyanq,huazhuangp,huazhuangq,wanjup,wanjuq,shoujip,shoujiq,jiajiup,jiajiuq,avp,avq,qichep,qicheq,guwanp,guwanq,fangzip,fangziq);
            save read = sqlService.read(user);
            saveService ss = new saveService(read);
            Gson gs = new Gson();
            String res = gs.toJson(ss);
            out = response.getWriter();
            out.println(res);
        } catch (Exception e) {
            out.print("error: data");
            e.printStackTrace();
        }

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }
}